home *** CD-ROM | disk | FTP | other *** search
/ Enter 2001 April / EnterCD4.iso / Update / SQL Server SP3 / sql70sp3i.exe / INSTALL / ansiview.sql next >
Encoding:
Text File  |  2000-09-06  |  23.5 KB  |  847 lines

  1.  
  2. -- - ---------- MSSQL 7.0  ANSIVIEW.SQL 1992 System Views
  3. set nocount on
  4. go
  5.  
  6. -- THESE ARE "SYSTEM" OBJECTS --
  7. exec sp_configure 'allow', 1
  8. reconfigure with override
  9. go
  10. exec sp_MS_upd_sysobj_category 1
  11. go
  12.  
  13. -- INFORMATION_SCHEMA login no longer exists...
  14. -- INFORMATION_SCHEMA user is added by hand here:
  15. if user_id('INFORMATION_SCHEMA') is NULL
  16.     INSERT sysusers VALUES (3, 0, 'INFORMATION_SCHEMA', NULL, 0x00, getdate(), getdate(), 0, NULL)
  17. go
  18. -- NO NEED TO GRANT CREATE VIEW TO INFORMATION_SCHEMA
  19.  
  20. if object_id('INFORMATION_SCHEMA.SCHEMATA', 'V') is not NULL
  21.     drop view INFORMATION_SCHEMA.SCHEMATA
  22.  
  23. if object_id('INFORMATION_SCHEMA.TABLES', 'V') is not NULL
  24.     drop view INFORMATION_SCHEMA.TABLES
  25.  
  26. if object_id('INFORMATION_SCHEMA.TABLE_CONSTRAINTS', 'V') is not NULL
  27.     drop view INFORMATION_SCHEMA.TABLE_CONSTRAINTS
  28.  
  29. if object_id('INFORMATION_SCHEMA.TABLE_PRIVILEGES', 'V') is not NULL
  30.     drop view INFORMATION_SCHEMA.TABLE_PRIVILEGES
  31.  
  32. if object_id('INFORMATION_SCHEMA.COLUMNS', 'V') is not NULL
  33.     drop view INFORMATION_SCHEMA.COLUMNS
  34.  
  35. if object_id('INFORMATION_SCHEMA.COLUMN_DOMAIN_USAGE', 'V') is not NULL
  36.     drop view INFORMATION_SCHEMA.COLUMN_DOMAIN_USAGE
  37.  
  38. if object_id('INFORMATION_SCHEMA.COLUMN_PRIVILEGES', 'V') is not NULL
  39.     drop view INFORMATION_SCHEMA.COLUMN_PRIVILEGES
  40.  
  41. if object_id('INFORMATION_SCHEMA.DOMAINS', 'V') is not NULL
  42.     drop view INFORMATION_SCHEMA.DOMAINS
  43.  
  44. if object_id('INFORMATION_SCHEMA.DOMAIN_CONSTRAINTS', 'V') is not NULL
  45.     drop view INFORMATION_SCHEMA.DOMAIN_CONSTRAINTS
  46.  
  47. if object_id('INFORMATION_SCHEMA.KEY_COLUMN_USAGE', 'V') is not NULL
  48.     drop view INFORMATION_SCHEMA.KEY_COLUMN_USAGE
  49.  
  50. if object_id('INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS', 'V') is not NULL
  51.     drop view INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS
  52.  
  53. if object_id('INFORMATION_SCHEMA.CHECK_CONSTRAINTS', 'V') is not NULL
  54.     drop view INFORMATION_SCHEMA.CHECK_CONSTRAINTS
  55.  
  56. if object_id('INFORMATION_SCHEMA.CONSTRAINT_TABLE_USAGE', 'V') is not NULL
  57.     drop view INFORMATION_SCHEMA.CONSTRAINT_TABLE_USAGE
  58.  
  59. if object_id('INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE', 'V') is not NULL
  60.     drop view INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
  61.  
  62. if object_id('INFORMATION_SCHEMA.VIEWS', 'V') is not NULL
  63.     drop view INFORMATION_SCHEMA.VIEWS
  64.  
  65. if object_id('INFORMATION_SCHEMA.VIEW_TABLE_USAGE', 'V') is not NULL
  66.     drop view INFORMATION_SCHEMA.VIEW_TABLE_USAGE
  67.  
  68. if object_id('INFORMATION_SCHEMA.VIEW_COLUMN_USAGE', 'V') is not NULL
  69.     drop view INFORMATION_SCHEMA.VIEW_COLUMN_USAGE
  70. go
  71.  
  72.  
  73. raiserror(15339,-1,-1,'INFORMATION_SCHEMA.SCHEMATA')
  74. go
  75.  
  76. --Identifies schmata owned by current users, databases current users has permissions in
  77. create view INFORMATION_SCHEMA.SCHEMATA
  78.  as
  79. select
  80.     db.name            as CATALOG_NAME
  81.     ,USER_NAME()    as SCHEMA_NAME
  82.     ,USER_NAME()    as SCHEMA_OWNER
  83.     ,'master'        as DEFAULT_CHARACTER_SET_CATALOG
  84.     ,'dbo'            as DEFAULT_CHARACTER_SET_SCHEMA
  85.     ,a_cha.name        as DEFAULT_CHARACTER_SET_NAME
  86. FROM
  87.     master.dbo.sysdatabases         db,
  88.     master.dbo.sysconfigures        cfg,
  89.     master.dbo.syscharsets        a_cha, --charset/1001, not sortorder.
  90.     master.dbo.syscharsets        b_cha --sortorder/2001, not charset.
  91. WHERE
  92.     cfg.comment = 'default sortorder id' 
  93.     AND a_cha.type = 1001 --- type is charset
  94.     AND b_cha.type = 2001 --- type is sortorder
  95.     AND a_cha.id = b_cha.csid
  96.     AND b_cha.id = cfg.value
  97. go
  98.  
  99. grant select on INFORMATION_SCHEMA.SCHEMATA to public
  100. go
  101.  
  102. raiserror(15339,-1,-1,'INFORMATION_SCHEMA.TABLES')
  103. go
  104. --Identifies tables accessible to the current user
  105. create view INFORMATION_SCHEMA.TABLES
  106. as 
  107. select  distinct
  108.     db_name()            as TABLE_CATALOG
  109.     ,user_name(o.uid)    as TABLE_SCHEMA
  110.     ,o.name                as TABLE_NAME
  111.     ,case o.xtype
  112.         when 'U' then 'BASE TABLE'
  113.         when 'V' then 'VIEW'
  114.     end                    as TABLE_TYPE
  115. from
  116.     sysobjects o
  117. where
  118.     o.xtype in ('U', 'V') and
  119.     permissions(o.id) != 0
  120. go
  121.  
  122. grant select on INFORMATION_SCHEMA.TABLES to public
  123. go
  124.  
  125. raiserror(15339,-1,-1,'INFORMATION_SCHEMA.TABLE_CONSTRAINTS')
  126. go
  127. --Identifies table constraints owned by current users
  128. create view INFORMATION_SCHEMA.TABLE_CONSTRAINTS
  129.  as
  130.  select
  131.     db_name()                as CONSTRAINT_CATALOG
  132.     ,user_name(c_obj.uid)    as CONSTRAINT_SCHEMA
  133.     ,c_obj.name                as CONSTRAINT_NAME
  134.     ,db_name()                as TABLE_CATALOG
  135.     ,user_name(t_obj.uid)    as TABLE_SCHEMA
  136.     ,t_obj.name                as TABLE_NAME
  137.     ,case c_obj.xtype
  138.                     when 'C' then    'CHECK'
  139.                     when 'UQ' then    'UNIQUE'
  140.                     when 'PK' then    'PRIMARY KEY'
  141.                     when 'F' then    'FOREIGN KEY'
  142.                    end        as CONSTRAINT_TYPE
  143.     ,'NO'                    as IS_DEFERRABLE
  144.     ,'NO'                    as INITIALLY_DEFERRED
  145. from
  146.     sysobjects    c_obj
  147.     ,sysobjects    t_obj
  148. where
  149.     c_obj.uid    = user_id()
  150.     and t_obj.id    = c_obj.parent_obj
  151.     and c_obj.xtype    in ('C' ,'UQ' ,'PK' ,'F')
  152. go
  153.  
  154. grant select on INFORMATION_SCHEMA.TABLE_CONSTRAINTS to public
  155. go
  156.  
  157. raiserror(15339,-1,-1,'INFORMATION_SCHEMA.TABLE_PRIVILEGES')
  158. go
  159. --Identifies privileges granted to or by the current user
  160. create view INFORMATION_SCHEMA.TABLE_PRIVILEGES
  161.  as
  162. select    
  163.     user_name(p.grantor)    as GRANTOR
  164.     ,user_name(p.uid)        as GRANTEE
  165.     ,db_name()                as TABLE_CATALOG
  166.     ,user_name(o.uid)        as TABLE_SCHEMA
  167.     ,o.name                    as TABLE_NAME
  168.     ,case p.action        
  169.         when 26  then 'REFERENCES'
  170.         when 193 then 'SELECT'
  171.         when 195 then 'INSERT'
  172.         when 196 then 'DELETE'
  173.         when 197 then 'UPDATE'
  174.     end                        as PRIVILEGE_TYPE
  175.     ,case 
  176.         when p.protecttype = 205 then 'NO'
  177.         else 'YES'
  178.     end                        as IS_GRANTABLE
  179.  from 
  180.     sysprotects p, 
  181.     sysobjects o
  182. where  
  183.     (is_member(user_name(p.uid)) = 1
  184.     or
  185.         p.grantor = user_id())
  186.      and (p.protecttype = 204 or     /*grant exists without same grant with grant */
  187.     (p.protecttype = 205
  188.         and not exists(select * from sysprotects p2
  189.                 where p2.id = p.id and
  190.                 p2.uid = p.uid and 
  191.                 p2.action = p.action and 
  192.                 p2.columns = p.columns and
  193.                 p2.grantor = p.grantor and
  194.                 p2.protecttype = 204)))
  195.      and p.action in (26,193,195,196,197)
  196.      and p.id = o.id
  197.     and o.xtype in ('U', 'V')
  198.      and 0 != (permissions(o.id) &
  199.         case p.action
  200.             when 26  then     4        /*REFERENCES basebit on all columns    */        
  201.             when 193 then     1        /*SELECT basebit on all columns    */        
  202.             when 195 then     8        /*INSERT basebit */
  203.             when 196 then     16        /*DELETE basebit */
  204.             when 197 then     2        /*UPDATE basebit on all columns    */
  205.         end)
  206. go
  207.  
  208. grant select on INFORMATION_SCHEMA.TABLE_PRIVILEGES to public
  209. go
  210.  
  211. raiserror(15339,-1,-1,'INFORMATION_SCHEMA.COLUMNS')
  212. go
  213.  
  214. --Displays columns accessable to the current user
  215. create view INFORMATION_SCHEMA.COLUMNS
  216.  as
  217. select 
  218.     db_name()                        as TABLE_CATALOG
  219.     ,user_name(obj.uid)                as TABLE_SCHEMA
  220.     ,obj.name                        as TABLE_NAME
  221.     ,col.name                        as COLUMN_NAME
  222.     ,col.colid                        as ORDINAL_POSITION
  223.     ,com.text                        as COLUMN_DEFAULT
  224.     ,case col.isnullable 
  225.         when 1 then 'YES'
  226.         else        'No '
  227.     end                                as IS_NULLABLE
  228.     ,spt_dtp.LOCAL_TYPE_NAME        as DATA_TYPE
  229.     ,col.length + spt_dtp.charbin    as CHARACTER_MAXIMUM_LENGTH
  230.     ,col.prec + spt_dtp.charbin        as CHARACTER_OCTET_LENGTH
  231.     ,nullif(col.xprec, 0)            as NUMERIC_PRECISION
  232.     ,spt_dtp.RADIX                    as NUMERIC_PRECISION_RADIX
  233.     ,col.scale                        as NUMERIC_SCALE
  234.     ,spt_dtp.SQL_DATETIME_SUB        as DATETIME_PRECISION
  235.     ,case 
  236.         when spt_dtp.LOCAL_TYPE_NAME in 
  237.          ('char', 'varchar', 'text', 'nchar', 'nvarchar', 'ntext')
  238.             then 'master'
  239.         else NULL
  240.     end                                as CHARACTER_SET_CATALOG
  241.     ,case
  242.         when spt_dtp.LOCAL_TYPE_NAME in 
  243.         ('char', 'varchar', 'text', 'nchar', 'nvarchar', 'ntext')
  244.             then 'dbo'
  245.         else NULL
  246.     end                                as CHARACTER_SET_SCHEMA
  247.     ,case
  248.         when spt_dtp.LOCAL_TYPE_NAME in 
  249.          ('char', 'varchar', 'text', 'nchar', 'nvarchar', 'ntext')
  250.             then a_cha.name
  251.         else NULL
  252.     end                                as CHARACTER_SET_NAME
  253.     ,case
  254.         when spt_dtp.LOCAL_TYPE_NAME in 
  255.         ('char', 'varchar', 'text', 'nchar', 'nvarchar', 'ntext')
  256.             then 'master'
  257.         else NULL
  258.     end                                as COLLATION_CATALOG
  259.     ,case
  260.         when spt_dtp.LOCAL_TYPE_NAME in 
  261.         ('char', 'varchar', 'text', 'nchar', 'nvarchar', 'ntext')
  262.             then 'dbo'
  263.         else NULL
  264.     end                                as COLLATION_SCHEMA
  265.     ,case
  266.         when spt_dtp.LOCAL_TYPE_NAME in 
  267.          ('char', 'varchar', 'text', 'nchar', 'nvarchar', 'ntext')
  268.             then b_cha.name
  269.         else NULL
  270.     end                                as COLLATION_NAME
  271.     ,case 
  272.         when typ.xusertype < 256  
  273.             then NULL
  274.         else DB_NAME()
  275.     end                                as DOMAIN_CATALOG
  276.     ,case 
  277.         when typ.xusertype < 256  
  278.             then NULL
  279.         else USER_NAME(obj.uid)
  280.     end                                as DOMAIN_SCHEMA
  281.     ,case 
  282.         when typ.xusertype < 256  
  283.             then NULL
  284.         else typ.name
  285.     end                                as DOMAIN_NAME
  286. FROM
  287.     sysobjects obj,
  288.     master.dbo.spt_datatype_info spt_dtp,
  289.     systypes typ,
  290.     syscolumns col
  291.     LEFT OUTER JOIN syscomments com on col.cdefault = com.id
  292.         AND com.colid = 1,
  293.     master.dbo.sysconfigures    cfg,
  294.     master.dbo.syscharsets        a_cha, --charset/1001, not sortorder.
  295.     master.dbo.syscharsets        b_cha --sortorder/2001, not charset.
  296. WHERE
  297.     permissions(obj.id, col.name) != 0
  298.     AND obj.id = col.id
  299.     AND typ.xtype = spt_dtp.ss_dtype
  300.     AND (spt_dtp.ODBCVer is null or spt_dtp.ODBCVer = 2)
  301.     AND obj.xtype in ('U', 'V')
  302.     AND col.xusertype = typ.xusertype
  303.     AND spt_dtp.AUTO_INCREMENT in ( 0, null)
  304.     AND cfg.comment = 'default sortorder id' 
  305.     AND a_cha.type = 1001 --- type is charset
  306.     AND b_cha.type = 2001 --- type is sortorder
  307.     AND a_cha.id = b_cha.csid
  308.     AND b_cha.id = cfg.value
  309. go
  310.  
  311. grant select on INFORMATION_SCHEMA.COLUMNS to public
  312. go
  313.  
  314. raiserror(15339,-1,-1,'INFORMATION_SCHEMA.COLUMN_DOMAIN_USAGE')
  315. go
  316.  
  317. --Identifies columns owned by current user that has a user defined datatype
  318. create view INFORMATION_SCHEMA.COLUMN_DOMAIN_USAGE
  319.  as
  320. select
  321.     db_name()            as DOMAIN_CATALOG
  322.     ,user_name(typ.uid)    as DOMAIN_SCHEMA
  323.     ,typ.name            as DOMAIN_NAME
  324.     ,db_name()            as TABLE_CATALOG
  325.     ,user_name(obj.uid)    as TABLE_SCHEMA
  326.     ,obj.name            as TABLE_NAME
  327.     ,col.name            as COLUMN_NAME
  328. FROM
  329.     sysobjects obj
  330.     ,syscolumns col
  331.     ,systypes typ 
  332. WHERE
  333.     obj.uid = user_id()
  334.     AND obj.id = col.id
  335.     AND col.xusertype = typ.xusertype
  336.     AND typ.xusertype > 256    -- UDF Type
  337.  
  338. go
  339.  
  340. grant select on INFORMATION_SCHEMA.COLUMN_DOMAIN_USAGE to public
  341. go
  342.  
  343. raiserror(15339,-1,-1,'INFORMATION_SCHEMA.COLUMN_PRIVILEGES')
  344. go
  345.  
  346. --Identifies privileges granted to or by current user
  347. create view INFORMATION_SCHEMA.COLUMN_PRIVILEGES
  348.  as
  349. select
  350.     user_name(p.grantor)    as GRANTOR
  351.     ,user_name(p.uid)               as GRANTEE
  352.     ,db_name()                              as TABLE_CATALOG
  353.     ,user_name(o.uid)               as TABLE_SCHEMA
  354.     ,o.name                                 as TABLE_NAME
  355.     ,c.name                                 as COLUMN_NAME
  356.     ,case p.action
  357.             when 193 then 'SELECT'
  358.             when 197 then 'UPDATE'
  359.             else 'REFERENCES'
  360.     end                                             as PRIVILEGE_TYPE
  361.     ,case
  362.             when p.protecttype = 205 then 'NO'
  363.             else 'YES'
  364.     end                                             as IS_GRANTABLE
  365.  from
  366.     sysprotects p,
  367.     sysobjects o,
  368.     syscolumns c
  369. where 
  370.     (is_member(user_name(p.uid)) = 1
  371.     or
  372.             p.grantor = user_id())
  373.     and (p.protecttype = 204 or     /*grant exists without same grant with grant */
  374.     (p.protecttype = 205
  375.             and not exists(select * from sysprotects p2
  376.                             where p2.id = p.id and
  377.                             p2.uid = p.uid and
  378.                             p2.action = p.action and
  379.                             p2.columns = p.columns and
  380.                             p2.grantor = p.grantor and
  381.                             p2.protecttype = 204)))
  382.     and p.action in (26,193,197)
  383.     and p.id = o.id
  384.     and o.xtype in ('U', 'V')
  385.     and o.id = c.id
  386.     and
  387.     (((convert(tinyint,substring(p.columns,1,1))&1) = 0
  388.             and
  389.     (convert(int,substring(p.columns,c.colid/8+1,1))&power(2,c.colid&7)) != 0)
  390.         or
  391.     ((convert(tinyint,substring(p.columns,1,1))&1) != 0
  392.             and 
  393.     (convert(int,substring(p.columns,c.colid/8+1,1))&power(2,c.colid&7)) = 0))
  394.     and 0 != (permissions
  395.             (o.id, c.name) &
  396.             case p.action
  397.                     when 26  then 4         /*REFERENCES basebit    */
  398.                     when 193 then 1         /*SELECT basebit        */
  399.                     when 197 then 2         /*UPDATE basebit        */
  400.                     end)
  401. go
  402.  
  403. grant select on INFORMATION_SCHEMA.COLUMN_PRIVILEGES to public
  404. go
  405.  
  406. raiserror(15339,-1,-1,'INFORMATION_SCHEMA.DOMAINS')
  407. go
  408.  
  409. --Identifies user defined datatype accessible to current user.
  410. create view INFORMATION_SCHEMA.DOMAINS
  411.  as
  412. select
  413.     DB_NAME()                        as DOMAIN_CATALOG
  414.     ,USER_NAME(typ.uid)                as DOMAIN_SCHEMA
  415.     ,typ.name                        as DOMAIN_NAME
  416.     ,spt_dtp.LOCAL_TYPE_NAME        as DATA_TYPE
  417.     ,typ.length + spt_dtp.charbin    as CHARACTER_MAXIMUM_LENGTH
  418.     ,typ.prec + spt_dtp.charbin        as CHARACTER_OCTET_LENGTH
  419.     ,case
  420.         when spt_dtp.LOCAL_TYPE_NAME in 
  421.         ('char', 'varchar', 'text', 'nchar', 'nvarchar', 'ntext')
  422.             then 'master'
  423.         else NULL
  424.     end                                as COLLATION_CATALOG
  425.     ,case
  426.         when spt_dtp.LOCAL_TYPE_NAME in 
  427.         ('char', 'varchar', 'text', 'nchar', 'nvarchar', 'ntext')
  428.             then 'dbo'
  429.         else NULL
  430.     end                                as COLLATION_SCHEMA
  431.     ,case
  432.         when spt_dtp.LOCAL_TYPE_NAME in 
  433.          ('char', 'varchar', 'text', 'nchar', 'nvarchar', 'ntext')
  434.             then b_cha.name
  435.         else NULL
  436.     end                                as COLLATION_NAME
  437.     ,case
  438.         when spt_dtp.LOCAL_TYPE_NAME in 
  439.          ('char', 'varchar', 'text', 'nchar', 'nvarchar', 'ntext')
  440.             then 'master'
  441.         else NULL
  442.     end                                as CHARACTER_SET_CATALOG
  443.     ,case
  444.         when spt_dtp.LOCAL_TYPE_NAME in 
  445.         ('char', 'varchar', 'text', 'nchar', 'nvarchar', 'ntext')
  446.             then 'dbo'
  447.         else NULL
  448.     end                                as CHARACTER_SET_SCHEMA
  449.     ,case
  450.         when spt_dtp.LOCAL_TYPE_NAME in 
  451.          ('char', 'varchar', 'text', 'nchar', 'nvarchar', 'ntext')
  452.             then a_cha.name
  453.         else NULL
  454.     end                                as CHARACTER_SET_NAME
  455.     ,nullif(typ.xprec, 0)            as NUMERIC_PRECISION
  456.     ,spt_dtp.RADIX                    as NUMERIC_PRECISION_RADIX
  457.     ,typ.scale                        as NUMERIC_SCALE
  458.     ,spt_dtp.SQL_DATETIME_SUB        as DATETIME_PRECISION
  459.     ,com.text                        as DOMAIN_DEFAULT
  460. FROM
  461.     master.dbo.spt_datatype_info spt_dtp,
  462.     systypes typ LEFT OUTER JOIN syscomments com
  463.          on typ.tdefault = com.id AND com.colid = 1,
  464.     master.dbo.sysconfigures    cfg,
  465.     master.dbo.syscharsets        a_cha, --charset/1001, not sortorder.
  466.     master.dbo.syscharsets        b_cha --sortorder/2001, not charset.
  467. WHERE
  468.      typ.xtype = spt_dtp.ss_dtype
  469.     AND (spt_dtp.ODBCVer is null or spt_dtp.ODBCVer = 2)    -- Use 7.0 entries
  470.         AND spt_dtp.AUTO_INCREMENT in ( 0, null)    -- Remove auto increment types
  471.     AND cfg.comment = 'default sortorder id' 
  472.     AND a_cha.type = 1001                     --- type is charset
  473.     AND b_cha.type = 2001                    -- type is sortorder
  474.     AND a_cha.id = b_cha.csid
  475.     AND b_cha.id = cfg.value
  476.     AND typ.xusertype > 256                    -- UDF Type
  477. go
  478.  
  479. grant select on INFORMATION_SCHEMA.DOMAINS to public
  480. go
  481.  
  482. raiserror(15339,-1,-1,'INFORMATION_SCHEMA.DOMAIN_CONSTRAINTS')
  483. go
  484.  
  485. --Identifies user defined datatype accessible to current user, that have constraints
  486. create view INFORMATION_SCHEMA.DOMAIN_CONSTRAINTS
  487.  as
  488. select
  489.     DB_NAME()            as CONSTRAINT_CATALOG
  490.     ,USER_NAME(obj.uid)    as CONSTRAINT_SCHEMA
  491.     ,obj.name            as CONSTRAINT_NAME
  492.     ,DB_NAME()            as DOMAIN_CATALOG
  493.     ,USER_NAME(typ.uid)    as DOMAIN_SCHEMA
  494.     ,typ.name            as DOMAIN_NAME
  495.     ,'NO'                as IS_DEFERRABLE
  496.     ,'NO'                as INITIALLY_DEFERRED
  497. FROM
  498.     sysobjects obj,
  499.     systypes typ 
  500. WHERE
  501.     obj.xtype = 'R'
  502.     and obj.id = typ.domain
  503.     AND typ.xusertype > 256    -- UDF Type
  504. go
  505.  
  506. grant select on INFORMATION_SCHEMA.DOMAIN_CONSTRAINTS to public
  507. go
  508.  
  509. raiserror(15339,-1,-1,'INFORMATION_SCHEMA.KEY_COLUMN_USAGE')
  510. go
  511.  
  512. --Identifies columns owned by current which are constrained key
  513. create view INFORMATION_SCHEMA.KEY_COLUMN_USAGE 
  514.  as
  515.  select
  516.     db_name()                as CONSTRAINT_CATALOG
  517.     ,user_name(c_obj.uid)    as CONSTRAINT_SCHEMA
  518.     ,c_obj.name                as CONSTRAINT_NAME
  519.     ,db_name()                as TABLE_CATALOG
  520.     ,user_name(t_obj.uid)    as TABLE_SCHEMA
  521.     ,t_obj.name                as TABLE_NAME
  522.     ,col.name                as COLUMN_NAME
  523.     ,case col.colid    
  524.         when ref.fkey1 then 1            
  525.         when ref.fkey2 then 2            
  526.         when ref.fkey3 then 3            
  527.         when ref.fkey4 then 4            
  528.         when ref.fkey5 then 5            
  529.         when ref.fkey6 then 6            
  530.         when ref.fkey7 then 7            
  531.         when ref.fkey8 then 8            
  532.         when ref.fkey9 then 9            
  533.         when ref.fkey10 then 10            
  534.         when ref.fkey11 then 11            
  535.         when ref.fkey12 then 12            
  536.         when ref.fkey13 then 13            
  537.         when ref.fkey14 then 14            
  538.         when ref.fkey15 then 15            
  539.         when ref.fkey16 then 16
  540.     end                        as ORDINAL_POSITION
  541. from
  542.     sysobjects    c_obj
  543.     ,sysobjects    t_obj
  544.     ,syscolumns    col
  545.     ,sysreferences  ref
  546. where
  547.     c_obj.uid   = user_id()
  548.     and c_obj.xtype    in ('F ')
  549.     and t_obj.id    = c_obj.parent_obj
  550.     and t_obj.id    = col.id
  551.     and col.colid   in 
  552.     (ref.fkey1,ref.fkey2,ref.fkey3,ref.fkey4,ref.fkey5,ref.fkey6,
  553.     ref.fkey7,ref.fkey8,ref.fkey9,ref.fkey10,ref.fkey11,ref.fkey12,
  554.     ref.fkey13,ref.fkey14,ref.fkey15,ref.fkey16)
  555.     and c_obj.id    = ref.constid
  556. union
  557.  select
  558.     db_name()                as CONSTRAINT_CATALOG
  559.     ,user_name(c_obj.uid)    as CONSTRAINT_SCHEMA
  560.     ,i.name                    as CONSTRAINT_NAME
  561.     ,db_name()                as TABLE_CATALOG
  562.     ,user_name(t_obj.uid)    as TABLE_SCHEMA
  563.     ,t_obj.name                as TABLE_NAME
  564.     ,col.name                as COLUMN_NAME
  565.     ,v.number                as ORDINAL_POSITION
  566. from
  567.     sysobjects        c_obj
  568.     ,sysobjects        t_obj
  569.     ,syscolumns        col
  570.     ,master.dbo.spt_values     v
  571.     ,sysindexes        i
  572. where
  573.     c_obj.uid    = user_id()
  574.     and c_obj.xtype    in ('UQ' ,'PK')
  575.     and t_obj.id    = c_obj.parent_obj
  576.     and t_obj.xtype  = 'U'
  577.     and t_obj.id    = col.id
  578.     and col.name    = index_col(t_obj.name,i.indid,v.number)
  579.     and t_obj.id    = i.id
  580.     and c_obj.name  = i.name
  581.     and v.number     > 0 
  582.      and v.number     <= i.keycnt 
  583.      and v.type     = 'P'
  584. go
  585.  
  586. grant select on INFORMATION_SCHEMA.KEY_COLUMN_USAGE to public
  587. go
  588.  
  589. raiserror(15339,-1,-1,'INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS')
  590. go
  591.  
  592. --Identifies foreign constraints owned by current user.
  593. create view INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS
  594.  as
  595.  select
  596.     db_name()                as CONSTRAINT_CATALOG
  597.     ,user_name(fc_obj.uid)    as CONSTRAINT_SCHEMA
  598.     ,fc_obj.name            as CONSTRAINT_NAME
  599.     ,db_name()                as UNIQUE_CONSTRAINT_CATALOG
  600.     ,user_name(pc_obj.uid)    as UNIQUE_CONSTRAINT_SCHEMA
  601.     ,i.name                    as UNIQUE_CONSTRAINT_NAME
  602.     ,'PARTIAL'                as MATCH_OPTION
  603.     ,'NO ACTION'            as UPDATE_RULE
  604.     ,'NO ACTION'            as DELETE_RULE
  605. from    
  606.     sysobjects    fc_obj
  607.     ,sysreferences    r
  608.     ,sysindexes    i
  609.     ,sysobjects    pc_obj
  610. where 
  611.     fc_obj.uid        = user_id()
  612.     and fc_obj.xtype    = 'F'
  613.     and r.constid        = fc_obj.id
  614.     and r.rkeyid        = i.id
  615.     and r.rkeyindid     = i.indid
  616.     and r.rkeyid        = pc_obj.id
  617. go
  618.  
  619. grant select on INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS to public
  620. go
  621.  
  622. raiserror(15339,-1,-1,'INFORMATION_SCHEMA.CHECK_CONSTRAINTS')
  623. go
  624.  
  625. --Identifies check constraints owned by current user
  626. create view INFORMATION_SCHEMA.CHECK_CONSTRAINTS
  627. as
  628. select
  629.     db_name()                as CONSTRAINT_CATALOG
  630.     ,user_name(c_obj.uid)    as CONSTRAINT_SCHEMA
  631.     ,c_obj.name                as CONSTRAINT_NAME
  632.     ,com.text                as CHECK_CLAUSE
  633. from
  634.     sysobjects    c_obj
  635.     ,syscomments    com
  636. where
  637.     c_obj.uid    = user_id()
  638.     and c_obj.id    = com.id
  639.     and c_obj.xtype    = 'C'
  640. go
  641.  
  642. grant select on INFORMATION_SCHEMA.CHECK_CONSTRAINTS to public
  643. go
  644.  
  645. raiserror(15339,-1,-1,'INFORMATION_SCHEMA.CONSTRAINT_TABLE_USAGE')
  646. go
  647.  
  648. --Identifies tables owned by the current user the have constraints 
  649. create view INFORMATION_SCHEMA.CONSTRAINT_TABLE_USAGE
  650.  as
  651.  select
  652.     db_name()                as TABLE_CATALOG
  653.     ,user_name(t_obj.uid)    as TABLE_SCHEMA
  654.     ,t_obj.name                as TABLE_NAME
  655.     ,db_name()                as CONSTRAINT_CATALOG
  656.     ,user_name(c_obj.uid)    as CONSTRAINT_SCHEMA
  657.     ,c_obj.name                as CONSTRAINT_NAME
  658. from
  659.     sysobjects    c_obj
  660.     ,sysobjects    t_obj
  661. where
  662.     t_obj.uid    = user_id()
  663.     and t_obj.id    = c_obj.parent_obj
  664.     and c_obj.xtype    in ('C' ,'UQ' ,'PK' ,'F')
  665. go
  666.  
  667. grant select on INFORMATION_SCHEMA.CONSTRAINT_TABLE_USAGE to public
  668. go
  669.  
  670. raiserror(15339,-1,-1,'INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE')
  671. go
  672.  
  673. --Identifies tables and columns owned by the current user the have constraints 
  674. create view INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
  675. (
  676. TABLE_CATALOG
  677. ,TABLE_SCHEMA
  678. ,TABLE_NAME
  679. ,COLUMN_NAME
  680. ,CONSTRAINT_CATALOG
  681. ,CONSTRAINT_SCHEMA
  682. ,CONSTRAINT_NAME
  683. )
  684.  as
  685.  select
  686.     KCU.TABLE_CATALOG    /*TABLE_CATALOG*/
  687.     ,KCU.TABLE_SCHEMA     /*TABLE_SCHEMA*/
  688.     ,KCU.TABLE_NAME     /*TABLE_NAME*/
  689.     ,KCU.COLUMN_NAME     /*COLUMN_NAME*/
  690.     ,KCU.CONSTRAINT_CATALOG/*CONSTRAINT_CATALOG*/
  691.     ,KCU.CONSTRAINT_SCHEMA /*CONSTRAINT_SCHEMA*/
  692.     ,KCU.CONSTRAINT_NAME     /*CONSTRAINT_NAME*/
  693. from    
  694.     INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU
  695. UNION
  696.  select
  697.     db_name()            /*TABLE_CATALOG*/
  698.     ,user_name(t_obj.uid)/*TABLE_SCHEMA*/
  699.     ,t_obj.name            /*TABLE_NAME*/
  700.     ,cols.name            /*COLUMN_NAME*/
  701.     ,db_name()            /*CONSTRAINT_CATALOG*/
  702.     ,user_name(c_obj.uid)/*CONSTRAINT_SCHEMA*/
  703.     ,c_obj.name            /*CONSTRAINT_NAME*/
  704. from    
  705.     sysobjects    t_obj
  706.     ,sysobjects    c_obj
  707.     ,syscolumns cols    
  708. where
  709.     t_obj.uid    = user_id()
  710.     and t_obj.id    = c_obj.parent_obj
  711.     and c_obj.xtype    = 'C'
  712.     and c_obj.info    = cols.colid
  713.     and cols.id        = c_obj.parent_obj
  714. UNION
  715. select
  716.     db_name()        /*TABLE_CATALOG*/
  717.     ,user_name(t_obj.uid)    /*TABLE_SCHEMA*/
  718.     ,t_obj.name        /*TABLE_NAME*/
  719.     ,col.name        /*COLUMN_NAME*/
  720.     ,db_name()        /*CONSTRAINT_CATALOG*/
  721.     ,user_name(r_obj.uid)    /*CONSTRAINT_SCHEMA*/
  722.     ,r_obj.name        /*CONSTRAINT_NAME*/
  723. FROM
  724.     sysobjects t_obj
  725.     ,syscolumns col
  726.     ,systypes typ 
  727.     ,sysobjects  r_obj
  728. WHERE
  729.     t_obj.uid = user_id()
  730.     AND t_obj.id = col.id
  731.     AND col.xusertype = typ.xusertype
  732.     AND typ.xusertype > 256    -- UDF Type
  733.     AND typ.domain = r_obj.id
  734.     AND r_obj.xtype = 'R'
  735. go
  736.  
  737. grant select on INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE to public
  738. go
  739.  
  740. raiserror(15339,-1,-1,'INFORMATION_SCHEMA.VIEWS')
  741. go
  742.  
  743. -- Displays views accessable to current user
  744. create view INFORMATION_SCHEMA.VIEWS
  745.  as
  746.  select
  747.     db_name()                as TABLE_CATALOG
  748.     ,user_name(obj.uid)        as TABLE_SCHEMA
  749.     ,obj.name                as TABLE_NAME
  750.     ,case
  751.         when exists (select * 
  752.             from syscomments com3
  753.             where com3.id = obj.id
  754.             and com3.colid > 1)    then NULL
  755.         else com.text
  756.     end                        as VIEW_DEFINITION
  757.     ,case
  758.         when exists (select *
  759.             from syscomments com2
  760.             where com2.id = obj.id 
  761.             and CHARINDEX('WITH CHECK OPTION',             
  762.             upper(com2.text)) > 0)  then 'CASCADE'
  763.         else 'NONE'
  764.     end                        as CHECK_OPTION
  765.     ,'NO'                    as IS_UPDATABLE
  766. from
  767.     sysobjects obj
  768.     ,syscomments com
  769. where   
  770.     permissions(obj.id) != 0
  771.     and obj.xtype    = 'V'
  772.     and obj.id      = com.id
  773.     and com.colid    = 1
  774. go
  775.  
  776. grant select on INFORMATION_SCHEMA.VIEWS to public
  777. go
  778.  
  779. raiserror(15339,-1,-1,'INFORMATION_SCHEMA.VIEW_TABLE_USAGE')
  780. go
  781.  
  782. -- Identifies views owned by current user and the tables used in their definition   
  783. create view INFORMATION_SCHEMA.VIEW_TABLE_USAGE
  784.  as
  785. select distinct
  786.     db_name()                as VIEW_CATALOG
  787.     ,user_name(v_obj.uid)    as VIEW_SCHEMA 
  788.     ,v_obj.name                as VIEW_NAME
  789.     ,db_name()                as TABLE_CATALOG
  790.     ,user_name(t_obj.uid)    as TABLE_SCHEMA
  791.     ,t_obj.name                as TABLE_NAME
  792. from
  793.      sysobjects    t_obj
  794.     ,sysobjects    v_obj
  795.     ,sysdepends    dep    
  796. where
  797.     v_obj.uid    = user_id()
  798.     and v_obj.xtype    = 'V'
  799.     and dep.id     = v_obj.id
  800.     and dep.depid    = t_obj.id
  801.  
  802. go
  803.  
  804. grant select on INFORMATION_SCHEMA.VIEW_TABLE_USAGE to public
  805. go
  806.  
  807. raiserror(15339,-1,-1,'INFORMATION_SCHEMA.VIEW_COLUMN_USAGE')
  808. go
  809.  
  810. -- Identifies views owned by current user and the columns used in their definition   
  811. create view INFORMATION_SCHEMA.VIEW_COLUMN_USAGE
  812.  as
  813. select
  814.     db_name()                as VIEW_CATALOG
  815.     ,user_name(v_obj.uid)    as VIEW_SCHEMA
  816.     ,v_obj.name                as VIEW_NAME
  817.     ,db_name()                as TABLE_CATALOG
  818.     ,user_name(t_obj.uid)    as TABLE_SCHEMA
  819.     ,t_obj.name                as TABLE_NAME
  820.     ,col.name                as COLUMN_NAME 
  821. from
  822.      sysobjects    t_obj
  823.     ,sysobjects    v_obj
  824.     ,sysdepends    dep
  825.     ,syscolumns col
  826.             
  827. where
  828.     v_obj.uid    = user_id()
  829.     and v_obj.xtype    = 'V'
  830.     and dep.id    = v_obj.id
  831.     and dep.depid    = t_obj.id
  832.     and t_obj.id    = col.id
  833.     and dep.depnumber    = col.colid
  834. go
  835.  
  836. grant select on INFORMATION_SCHEMA.VIEW_COLUMN_USAGE to public
  837. go
  838.  
  839.  
  840.  
  841. -- END OF "SYSTEM" OBJECT CREATION --
  842. exec sp_MS_upd_sysobj_category 2
  843. go
  844. exec sp_configure 'allow', 0
  845. reconfigure with override
  846. go
  847.